home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / os2 / octa209s.zip / octave-2.09 / scripts / linear-algebra / vech.m < prev   
Text File  |  1997-03-07  |  2KB  |  52 lines

  1. ## Copyright (C) 1995, 1996  Kurt Hornik
  2. ## 
  3. ## This program is free software; you can redistribute it and/or modify
  4. ## it under the terms of the GNU General Public License as published by
  5. ## the Free Software Foundation; either version 2, or (at your option)
  6. ## any later version.
  7. ## 
  8. ## This program is distributed in the hope that it will be useful, but
  9. ## WITHOUT ANY WARRANTY; without even the implied warranty of
  10. ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  11. ## General Public License for more details. 
  12. ## 
  13. ## You should have received a copy of the GNU General Public License
  14. ## along with this file.  If not, write to the Free Software Foundation,
  15. ## 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  16.  
  17. ## usage: vech (x)
  18. ##
  19. ## For square x, returns the vector vech (x) which is obtained from x
  20. ## by eliminating all supradiagonal elements and stacking the result
  21. ## one column above the other.
  22. ## 
  23. ## See Magnus and Neudecker (1988), Matrix differential calculus with
  24. ## applications in statistics and econometrics.
  25.  
  26. ## Author KH <Kurt.Hornik@ci.tuwien.ac.at>
  27. ## Created: 8 May 1995
  28. ## Adapted-By: jwe
  29.  
  30. function v = vech (x)
  31.   
  32.   if (nargin != 1)
  33.     usage ("vech (x)");
  34.   endif
  35.   
  36.   if (! is_square (x))
  37.     error ("vech:  x must be square");
  38.   endif
  39.   
  40.   ## This should be quicker than having an inner `for' loop as well.
  41.   ## Ideally, vech should be written in C++.
  42.   n = rows (x);
  43.   v = zeros ((n+1)*n/2, 1);
  44.   count = 0;
  45.   for j = 1 : n
  46.     i = j : n; 
  47.     v (count + i) = x (i, j);
  48.     count = count + n - j;
  49.   endfor
  50.  
  51. endfunction
  52.